package leetcode.力扣竞赛.第98场双周赛;

import java.util.ArrayList;
import java.util.List;

/**
 * 修改两个元素的最小分数
 *
 * @author: yan
 * @description:
 * @create: 2023-02-20 16:21
 **/
public class Solution2567 {

    public static void main(String[] args) {
        int[] arr = new int[]{58,42,8,75,28};
        System.out.println(minimizeSum(arr));
    }

    public static int minimizeSum(int[] nums) {
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE, min3 = Integer.MAX_VALUE, max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, res1, res2, res3;
        int maxScout = 0, minScout = 0;
        List<Integer> list = new ArrayList<>();
        for (int num : nums){
            list.add(num);
        }
        list.remove(getMinOrMaxPozition(list, 0));
        list.remove(getMinOrMaxPozition(list, 0));
        for (int num : list) {
            if (num > max1) {
                max1 = num;
            }
            if (num < min1) {
                min1 = num;
            }
        }
        res1 = Math.abs(max1 - min1);
        list = new ArrayList<>();
        for (int num : nums){
            list.add(num);
        }
        list.remove(getMinOrMaxPozition(list, 1));
        list.remove(getMinOrMaxPozition(list, 1));
        for (int num : list) {
            if (num > max2) {
                max2 = num;
            }
            if (num < min2) {
                min2 = num;
            }
        }
        res2 = Math.abs(max2 - min2);
        list = new ArrayList<>();
        for (int num : nums){
            list.add(num);
        }
        list.remove(getMinOrMaxPozition(list, 0));
        list.remove(getMinOrMaxPozition(list, 1));
        for (int num : list) {
            if (num > max3) {
                max3 = num;
            }
            if (num < min3) {
                min3 = num;
            }
        }
        res3 = Math.abs(max3 - min3);
        return Math.min(Math.min(res1, res2), res3);
    }

    private static int getMinOrMaxPozition(List<Integer> list, int minOrmax){
        int res = 0, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
        if (minOrmax == 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) > max) {
                    res = i;
                    max = list.get(i);
                }
            }
        }
        if (minOrmax == 1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) < min) {
                    res = i;
                    min = list.get(i);
                }
            }
        }
        return res;
    }

}
